package com.asset.salesforce.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.Company;
import com.asset.salesforce.domain.Employee;

@Repository
public class CompanyDaoImpl {
	@Autowired
	private SessionFactory sessionFactory;
	
	public void addCompany(Company company) {
		try{
		sessionFactory.getCurrentSession().save(company);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Company> getAllCompany() {
		
		return sessionFactory.getCurrentSession().createQuery("from Company").list();
		
		
	}

	public void deleteCompany(Integer id) {
		Company company = (Company) sessionFactory.getCurrentSession().load(
				Company.class, id);
		if (null != company) {
			sessionFactory.getCurrentSession().delete(company);
		}
		
	}
	public Company getCompanyByName(String companyName){
		@SuppressWarnings("unchecked")
		List<Company> companies = sessionFactory.getCurrentSession().createQuery("from Company comp where comp.companyName = :companyName" ).setParameter("companyName", companyName).list();
		if(companies.size() >0){
			 return companies.get(0);
		 }else{
			 return null;
		 }
	}
	public  List<Employee> getActivatedEmployeesByCompanyName(String companyName){
		
		return  sessionFactory.getCurrentSession().createQuery("from Employee emp where emp.company.companyName = :companyName and emp.isActive = :is"  ).setParameter("companyName", companyName).setParameter("is", true).list();
//		if(companies.size() >0){
//			 return companies.get(0);
//		 }else{
//			 return null;
//		 }
	}
	

}
